package com.ssii.www.myproject.web.controller;

import com.ssii.www.myproject.common.util.AjaxResult;
import com.ssii.www.myproject.common.util.LogUtils;
import com.ssii.www.myproject.domain.Mylog;
import com.ssii.www.myproject.query.MylogQuery;
import com.ssii.www.myproject.service.MylogService;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/log")
public class MylogController extends BaseController {
  private static final Logger logger = LogUtils.getLogger();
  @Autowired
  private MylogService logService;

  @RequiresPermissions("log:index")
  @RequestMapping("/index")
  public String index() {
    return "log/index";
  }

  @RequestMapping("/page")
  @ResponseBody
  public Map<String, Object> page(MylogQuery query) {
    Map<String, Object> map = new HashMap<>();
    Page<Mylog> pageByQuery = logService.findPageByQuery(query);
    map.put("rows", pageByQuery.getContent());
    map.put("page", pageByQuery.getTotalPages());
    map.put("total", pageByQuery.getTotalElements());
    return map;
  }

  @ResponseBody
  @RequestMapping("/deleteAll")
  public AjaxResult deleteAll() {
    AjaxResult ajaxResult = new AjaxResult();
    try {
      logService.deleteAll();
      ajaxResult.setSuccess(true);
    } catch (Exception e) {
      logger.error("",e);
      ajaxResult.setSuccess(false);
      ajaxResult.setMessage("删除失败，原因：" + e.getMessage());
    }
    return ajaxResult;
  }

  @ResponseBody
  @RequestMapping("/delete")
  public AjaxResult delete( long[] data) {
    AjaxResult ajaxResult = new AjaxResult();
    try {
      for (int i = 0; i < data.length; i++) {
        logService.delete(data[i]);
        System.out.println(data[i]);
      }
      System.out.println(Arrays.toString(data));
      ajaxResult.setSuccess(true);
    } catch (Exception e) {
      logger.error("",e);
      ajaxResult.setSuccess(false);
      ajaxResult.setMessage("删除失败，原因：" + e.getMessage());
    }
    return ajaxResult;
  }
}
